home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / wsgiref / util.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  6KB  |  172 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Miscellaneous WSGI-related Utilities'''
  5. import posixpath
  6. __all__ = [
  7.     'FileWrapper',
  8.     'guess_scheme',
  9.     'application_uri',
  10.     'request_uri',
  11.     'shift_path_info',
  12.     'setup_testing_defaults']
  13.  
  14. class FileWrapper:
  15.     '''Wrapper to convert file-like objects to iterables'''
  16.     
  17.     def __init__(self, filelike, blksize = 8192):
  18.         self.filelike = filelike
  19.         self.blksize = blksize
  20.         if hasattr(filelike, 'close'):
  21.             self.close = filelike.close
  22.         
  23.  
  24.     
  25.     def __getitem__(self, key):
  26.         data = self.filelike.read(self.blksize)
  27.         if data:
  28.             return data
  29.         raise IndexError
  30.  
  31.     
  32.     def __iter__(self):
  33.         return self
  34.  
  35.     
  36.     def next(self):
  37.         data = self.filelike.read(self.blksize)
  38.         if data:
  39.             return data
  40.         raise StopIteration
  41.  
  42.  
  43.  
  44. def guess_scheme(environ):
  45.     """Return a guess for whether 'wsgi.url_scheme' should be 'http' or 'https'
  46.     """
  47.     if environ.get('HTTPS') in ('yes', 'on', '1'):
  48.         return 'https'
  49.     return 'http'
  50.  
  51.  
  52. def application_uri(environ):
  53.     """Return the application's base URI (no PATH_INFO or QUERY_STRING)"""
  54.     url = environ['wsgi.url_scheme'] + '://'
  55.     quote = quote
  56.     import urllib
  57.     if environ.get('HTTP_HOST'):
  58.         url += environ['HTTP_HOST']
  59.     else:
  60.         url += environ['SERVER_NAME']
  61.         if environ['wsgi.url_scheme'] == 'https':
  62.             if environ['SERVER_PORT'] != '443':
  63.                 url += ':' + environ['SERVER_PORT']
  64.             
  65.         elif environ['SERVER_PORT'] != '80':
  66.             url += ':' + environ['SERVER_PORT']
  67.         
  68.     if not environ.get('SCRIPT_NAME'):
  69.         pass
  70.     url += quote('/')
  71.     return url
  72.  
  73.  
  74. def request_uri(environ, include_query = 1):
  75.     '''Return the full request URI, optionally including the query string'''
  76.     url = application_uri(environ)
  77.     quote = quote
  78.     import urllib
  79.     path_info = quote(environ.get('PATH_INFO', ''))
  80.     if not environ.get('SCRIPT_NAME'):
  81.         url += path_info[1:]
  82.     else:
  83.         url += path_info
  84.     if include_query and environ.get('QUERY_STRING'):
  85.         url += '?' + environ['QUERY_STRING']
  86.     
  87.     return url
  88.  
  89.  
  90. def shift_path_info(environ):
  91.     """Shift a name from PATH_INFO to SCRIPT_NAME, returning it
  92.  
  93.     If there are no remaining path segments in PATH_INFO, return None.
  94.     Note: 'environ' is modified in-place; use a copy if you need to keep
  95.     the original PATH_INFO or SCRIPT_NAME.
  96.  
  97.     Note: when PATH_INFO is just a '/', this returns '' and appends a trailing
  98.     '/' to SCRIPT_NAME, even though empty path segments are normally ignored,
  99.     and SCRIPT_NAME doesn't normally end in a '/'.  This is intentional
  100.     behavior, to ensure that an application can tell the difference between
  101.     '/x' and '/x/' when traversing to objects.
  102.     """
  103.     path_info = environ.get('PATH_INFO', '')
  104.     if not path_info:
  105.         return None
  106.     path_parts = path_info.split('/')
  107.     path_parts[1:-1] = _[1]
  108.     name = path_parts[1]
  109.     del path_parts[1]
  110.     script_name = environ.get('SCRIPT_NAME', '')
  111.     script_name = posixpath.normpath(script_name + '/' + name)
  112.     if not name and not script_name.endswith('/'):
  113.         script_name += '/'
  114.     
  115.     environ['SCRIPT_NAME'] = script_name
  116.     environ['PATH_INFO'] = '/'.join(path_parts)
  117.     if name == '.':
  118.         name = None
  119.     
  120.     return name
  121.  
  122.  
  123. def setup_testing_defaults(environ):
  124.     """Update 'environ' with trivial defaults for testing purposes
  125.  
  126.     This adds various parameters required for WSGI, including HTTP_HOST,
  127.     SERVER_NAME, SERVER_PORT, REQUEST_METHOD, SCRIPT_NAME, PATH_INFO,
  128.     and all of the wsgi.* variables.  It only supplies default values,
  129.     and does not replace any existing settings for these variables.
  130.  
  131.     This routine is intended to make it easier for unit tests of WSGI
  132.     servers and applications to set up dummy environments.  It should *not*
  133.     be used by actual WSGI servers or applications, since the data is fake!
  134.     """
  135.     environ.setdefault('SERVER_NAME', '127.0.0.1')
  136.     environ.setdefault('SERVER_PROTOCOL', 'HTTP/1.0')
  137.     environ.setdefault('HTTP_HOST', environ['SERVER_NAME'])
  138.     environ.setdefault('REQUEST_METHOD', 'GET')
  139.     if 'SCRIPT_NAME' not in environ and 'PATH_INFO' not in environ:
  140.         environ.setdefault('SCRIPT_NAME', '')
  141.         environ.setdefault('PATH_INFO', '/')
  142.     
  143.     environ.setdefault('wsgi.version', (1, 0))
  144.     environ.setdefault('wsgi.run_once', 0)
  145.     environ.setdefault('wsgi.multithread', 0)
  146.     environ.setdefault('wsgi.multiprocess', 0)
  147.     StringIO = StringIO
  148.     import StringIO
  149.     environ.setdefault('wsgi.input', StringIO(''))
  150.     environ.setdefault('wsgi.errors', StringIO())
  151.     environ.setdefault('wsgi.url_scheme', guess_scheme(environ))
  152.     if environ['wsgi.url_scheme'] == 'http':
  153.         environ.setdefault('SERVER_PORT', '80')
  154.     elif environ['wsgi.url_scheme'] == 'https':
  155.         environ.setdefault('SERVER_PORT', '443')
  156.     
  157.  
  158. _hoppish = {
  159.     'connection': 1,
  160.     'keep-alive': 1,
  161.     'proxy-authenticate': 1,
  162.     'proxy-authorization': 1,
  163.     'te': 1,
  164.     'trailers': 1,
  165.     'transfer-encoding': 1,
  166.     'upgrade': 1 }.__contains__
  167.  
  168. def is_hop_by_hop(header_name):
  169.     '''Return true if \'header_name\' is an HTTP/1.1 "Hop-by-Hop" header'''
  170.     return _hoppish(header_name.lower())
  171.  
  172.